import requests

# 基础URL
base_url = "https://movie.douban.com/top250"
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
}
proxies = {
    "http": None,
    "https": None
}

# 用于存储所有页面的内容
all_content = ""

# 循环请求每一页
for start in range(0, 250, 25):
    # 构建当前页的URL
    url = f"{base_url}?start={start}"
    try:
        response = requests.get(url=url, headers=headers, proxies=proxies)
        response.raise_for_status()  # 检查请求是否成功
        # 将当前页的内容添加到总内容中
        all_content += response.text
        print(f"成功获取第 {start // 25 + 1} 页的内容")
    except requests.RequestException as e:
        print(f"请求第 {start // 25 + 1} 页时出错: {e}")

# 将所有页面的内容写入文件
with open("douban_all_pages.html", "w", encoding="utf-8") as f:
    f.write(all_content)